package com.seveneight.baijiesteward.views;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.http.AppConfig;
import com.seveneight.baijiesteward.utils.DensityUtil;
import com.seveneight.baijiesteward.utils.GlideLoader;
import com.seveneight.baijiesteward.utils.ToastUtil;

import java.util.List;

/**
 * Created by user1 on 2018/3/27.
 */

public class CustomerDialog extends Dialog {

    private View alertView;
    private Context context;
    private AnimationSet mModalInAnim;
    private AnimationSet mModalOutAnim;

    public CustomerDialog(Context context) {
        super(context, R.style.Dialog_Alert_Theme);
        this.context = context;
        alertView = LayoutInflater.from(context).inflate(R.layout.dialog_customer, null);
        setContentView(alertView);
        initView();
    }

    private void initView() {
        alertView.getLayoutParams().width = (int) (DensityUtil.getWindowWidth(context) * 0.7);
        TextView tv_cancle = (TextView) findViewById(R.id.tv_cancle);
        TextView tv_go = (TextView) findViewById(R.id.tv_go);
        ImageView iv_bg = (ImageView) findViewById(R.id.iv_bg);
        GlideLoader.displayImg(context, R.mipmap.bg_customer, iv_bg);

        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        tv_go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isQQClientAvailable(context)) {
                    context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(AppConfig.QQ)));
                } else {
                    ToastUtil.show(context, "请安装手机QQ");
                }
                dismiss();
            }
        });
    }

    private void initAnimation() {
        mModalInAnim = (AnimationSet) AnimationUtils.loadAnimation(getContext(), R.anim.dialog_modal_in);
        mModalOutAnim = (AnimationSet) AnimationUtils.loadAnimation(getContext(), R.anim.dialog_modal_out);
        mModalOutAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // alertView.setVisibility(View.GONE);
                alertView.post(new Runnable() {
                    @Override
                    public void run() {
                        CustomerDialog.super.dismiss();
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    @Override
    public void show() {
        alertView.setVisibility(View.VISIBLE);
        super.show();
    }

    public void dismiss() {
        super.dismiss();
    }

    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equalsIgnoreCase("com.tencent.qqlite") || pn.equalsIgnoreCase("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }
}
